package com.tmc.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;

import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.entity.common.EthDept;
import com.tmc.util.ObjectUtil;

@Repository
public class DeptDaoImpl extends BaseDaoImpl<EthDept>{
	public Pagination<EthDept> findByCon(ContextInfo context, EthDept cond, Integer pageIndex, Integer pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthDept c where c.enabledFlag = 'Y' ");
		if (ObjectUtil.hasText(cond.getDeptName())) {
			sql.append(" and (c.deptName like :deptName or c.deptCode like :deptName)");
			params.put("deptName", "%" + cond.getDeptName() + "%");
		}
		
		sql.append(" order by c.lastUpdateDate desc ");
		return this.findPagination(sql.toString(), params, pageIndex, pageSize);
	}

	public List<EthDept> findListByCon(ContextInfo context, EthDept cond, int pageIndex, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthDept c where c.enabledFlag = 'Y' ");
		if (ObjectUtil.hasText(cond.getDeptName())) {
			sql.append(" and c.deptName like :deptName ");
			params.put("deptName", "%" + cond.getDeptName() + "%");
		}
		
		sql.append(" order by c.lastUpdateDate desc ");
		return this.findList(sql.toString(), params);
	}

	public List<EthDept> findAll(ContextInfo context) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthDept c where c.enabledFlag = 'Y' ");
		return this.findList(sql.toString(), params);
	}

	public EthDept getByCode(ContextInfo context, String code) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthDept c where c.enabledFlag = 'Y' ");
		sql.append(" and c.deptCode = :deptCode ");
		params.put("deptCode", code);
		return this.get(sql.toString(), params);
	}

	public EthDept getByName(ContextInfo context, String name) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthDept c where c.enabledFlag = 'Y' ");
		sql.append(" and c.deptName = :deptName ");
		params.put("deptName", name);
		return this.get(sql.toString(), params);
	}

	public List<EthDept> findCurrentDepts(ContextInfo context) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthDept c where c.enabledFlag = 'Y' ");
		sql.append(" order by c.lastUpdateDate desc ");
		return this.findList(sql.toString(), params);
	}

	public List<EthDept> findListByName(String deptName) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthDept c where c.enabledFlag = 'Y' ");
		sql.append(" and c.deptName like :deptName ");
		params.put("deptName", "%"+deptName+"%");
		return this.findList(sql.toString(), params);
	}
	
}
